package net.edu_soft.ec.asset.service.impl;

import net.edu_soft.ec.asset.mapper.UsageRecordMapper;
import net.edu_soft.ec.asset.pojo.UsageRecord;
import net.edu_soft.ec.asset.pojo.UsageRecordExample;
import net.edu_soft.ec.asset.service.UsageRecordService;
import net.edu_soft.ec.common.exception.OperationException;
import net.edu_soft.ec.common.util.PuingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class UsageRecordServiceImpl implements UsageRecordService {

    @Autowired
    private UsageRecordMapper usageRecordMapper;

    @Override
    public UsageRecord findByProductAndSchoolId(Integer productId, Integer schoolId) {
        return selByPIAndSId(productId,schoolId);
    }

    @Override
    public UsageRecord findById(Integer usageRecordId) {
        return selById(usageRecordId);
    }

    @Override
    public int save(UsageRecord usageRecord) {
        //查询是否已添加
        UsageRecord selUsageRecord = selByPIAndSId(usageRecord.getProductId(), usageRecord.getSchoolId());
        if(selUsageRecord!=null){
            throw new OperationException("高校使用记录：添加失败，存在该记录");
        }
        //添加
        int i = usageRecordMapper.insertSelective(usageRecord);
        if(i<=0){
            throw new OperationException("使用记录：添加失败，请检查添加业务或参数或数据");
        }
        return i;
    }

    @Override
    public int saveList(List<UsageRecord> usageRecords) {


        return 0;
    }

    @Override
    public int update(UsageRecord usageRecord) {
        int i = 0;
        //判断是否存在该id，不存在则为添加
        if(usageRecord.getUsageRecordId()==null){
            i = save(usageRecord);
        }else {
            //修改，传入id查询
            UsageRecord selUsageRecord = selById(usageRecord.getUsageRecordId());
            //是否存在
            if(selUsageRecord==null){
                throw new OperationException("高校使用记录：修改失败，不存在该记录");
            }
            //修改
            i = usageRecordMapper.updateByPrimaryKeySelective(usageRecord);
        }
        if(i<=0){
            throw new OperationException("使用记录：修改失败，请检查修改业务或参数或数据");
        }
        return i;
    }

    @Override
    public int updateList(List<UsageRecord> usageRecords) {

        return 0;
    }

    @Override
    public int del(Integer productId, Integer isDel) {
        PuingUtil.notNullByZero(productId,"使用记录：产品id不能为空或者小于等于0");
        if(isDel==1){
            UsageRecordExample example = new UsageRecordExample();
            //添加产品id
            example.createCriteria().andProductIdEqualTo(productId);
            List<UsageRecord> usageRecords = usageRecordMapper.selectByExample(example);
            if(usageRecords.size()>0 && usageRecords!=null){
                PuingUtil.notNullByList(usageRecords,"使用记录：根据产品id查询不到该记录");
                int i = usageRecordMapper.deleteByExample(example);
                if(i<=0){
                    throw new OperationException("使用记录：删除失败，请检查删除业务或参数或数据");
                }
            }
        }
        return 0;
    }

    @Override
    public int delList(String ids, Integer isDel) {
        int i=0;
        Assert.hasLength(ids,"使用记录：id串不能为空");
        if(isDel==1){
            List<Integer> idList = PuingUtil.getNumber(ids);
            PuingUtil.notNullByList(idList,"使用记录：id串不能为空");
            for (Integer id: idList) {
                UsageRecord usageRecord = selById(id);
                if(usageRecord==null){
                    throw new OperationException("高校使用记录：该id："+id+"查询失败，不存在该记录");
                }
                i = usageRecordMapper.deleteByPrimaryKey(id);
                if(i<=0){
                    throw new OperationException("使用记录：删除失败，请检查删除业务或参数或数据");
                }
            }
        }
        return i;
    }

    /***
     * 根据产品id和高效id查询信息
     * @param productId 产品id
     * @param schoolId 高校id
     * @return
     */
    private UsageRecord selByPIAndSId(Integer productId, Integer schoolId){
        PuingUtil.notNullByInt("使用记录：产品id或高效id不能为空或小于等于0",productId,schoolId);
        UsageRecordExample example = new UsageRecordExample();
        UsageRecordExample.Criteria criteria = example.createCriteria();
        //添加产品id
        criteria.andProductIdEqualTo(productId);
        //添加高校id
        criteria.andSchoolIdEqualTo(schoolId);
        List<UsageRecord> usageRecords = usageRecordMapper.selectByExample(example);
        if(usageRecords.size()>0){
            return usageRecords.get(0);
        }
        return null;
    }


    /***
     * 根据记录id查询记录
     * @param usageRecordId 记录id
     * @return
     */
    private UsageRecord selById(Integer usageRecordId){
        PuingUtil.notNullByZero(usageRecordId,"使用记录：记录id不能为空或者小于等于0");
        UsageRecord usageRecord = usageRecordMapper.selectByPrimaryKey(usageRecordId);
        return usageRecord;
    }

}
